<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2021/4/23
 * Time: 13:24
 */

namespace ticket\modules\frontend\controllers;


use ticket\libs\Curl;
use ticket\libs\Tools;
use yii\log\FileTarget;
use yii\web\Controller;
use Yii;
use yii\base\Exception;

class XlCallbackController extends Controller
{

    private $key = 'e078YQWNQ3NThsv1QPEjTi93YSoLMvDk';   // 支付中心 恐龙园加密kek
    private $url = 'https://mp.52qmp.com';
    private $clientId = 'client1';
    private $clientSecret= '6ca84118222dd946';
    private $partnerId = '266';
    private $actions = 'payOrder';


    /*** 恐龙园VIP购票，订单通知
     * @return false|string
     */
    public function actionXlTickets()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            try{
                $res = $this->checkSign($post,$this->key);
                if ($res and $post['payStatus'] == '00'){
                    $url = $this->url."/client/standard/payOrder";
                    $attachInfo = $post['attach'];
                    if (!empty($attachInfo)){
                        $paramsData = [
                            'body' => [
                                'orderNo' => $attachInfo['orderNo'],
                                'otaOrderNo' => $attachInfo['otaOrderNo'],
                            ],
                            'partnerId' => $this->partnerId,
                            'sign' => $this->getSign(),
                            'timestamp' => Tools::get_msectime(),
                        ];
                        Curl::setHeader(['Content-Type' => 'application/json;charset=UTF-8']);
                        $result = json_decode(Curl::post($url,json_encode($paramsData)),true);
                        if ($result['code'] != 1){
                            $this->recordText(9999,'订单通知失败--'.json_encode($result,JSON_UNESCAPED_UNICODE),json_encode($paramsData,JSON_UNESCAPED_UNICODE));
                            throw new Exception('订单通知失败');
                        }
                        $this->recordText(0,'订单通知成功'.json_encode($result,JSON_UNESCAPED_UNICODE),json_encode($paramsData,JSON_UNESCAPED_UNICODE));
                        $params = [
                            'relCode' => 0,
                            'msg' => '请求成功',
                        ];
                        return json_encode($params);
                    }
                }
                throw new Exception('签名校验失败--'.$res);
            }catch (Exception $e){
                $this->recordText(99999,$e->getMessage(),json_encode($post,JSON_UNESCAPED_UNICODE));
                return json_encode(['relCode' => 99,'msg' => $e->getMessage()]);
            }

        }
    }

    /*** 获取签名
     * @return string
     */
    private function getSign()
    {
        $sign1 = strtoupper(md5($this->clientId.$this->actions));
        $sign2 = strtoupper(md5($sign1.$this->partnerId));
        $sign = strtoupper(md5($sign2.$this->clientSecret));
        return $sign;
    }


    /*** 支付回调参数校验
     * @param $data
     * @return bool
     */
    private function checkSign($data,$key)
    {

        $sign = $data['sign'];
        $postSign = Tools::computeSign($data,$key);
        if ($sign == $postSign){
            return true;
        }
        throw new Exception('签名校验失败'.$sign.'--'.$postSign);
    }


    protected function recordText($code,$msg,$data)
    {
        $log = new FileTarget();
        $log->logFile = \Yii::$app->getRuntimePath() . '/xl_callback/logs_' . date('Ymd') . '.log';
        $log->messages[] = [
            "[url:{$_SERVER['REQUEST_URI']}],[data:{$code},{$msg},{$data}]",
            1,
            'application',
            time(),
        ];
        $log->export();
    }



}